package com.hd.appcommon.utils;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Administrator on 2018/12/26.
 */
public class RequestUtils {

    public static boolean isDevtools(HttpServletRequest request) {
        String agent = request.getHeader("user-agent");
        return agent.contains("wechatdevtools");
    }

    /**
     * java获取客户端
     */
    public static String getPlatform(HttpServletRequest request) {

        /**User Agent中文名为用户代理，简称 UA，它是一个特殊字符串头，使得服务器
         能够识别客户使用的操作系统及版本、CPU 类型、浏览器及版本、浏览器渲染引擎、浏览器语言、浏览器插件等*/
        String agent = request.getHeader("user-agent");
        //客户端类型常量
        String type = "";
        if (agent.contains("Windows")) {
            type = "window";
        } else if (agent.contains("Mac")) {
            type = "mac";
        } else if (agent.contains("Linux")) {
            type = "linux";
        }
        return type;
    }


    public static HttpServletRequest getRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }

    public static boolean isAjaxRequest() {
        HttpServletRequest request = getRequest();
        return "XMLHttpRequest".equalsIgnoreCase(request.getHeader("X-Requested-With"));
    }

    public static String getRequestMethod() {
        HttpServletRequest request = getRequest();
        String method = request.getMethod();
        return method;
    }

    public static String getRequestMethod(HttpServletRequest request) {
        String method = request.getMethod();
        return method;
    }
}
